home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #14 / Monster Media No. 14 (April 1996) (Monster Media, Inc.).ISO / games_a / wordy410.zip / INSET.C < prev    next >
C/C++ Source or Header  |  1995-12-16  |  6KB  |  227 lines

  1. /**************************************************************************/
  2. /*                             Inset   Utility                            */
  3. /*                                                                        */
  4. /*                                 M\Cooper                               */
  5. /*                        3425 Chestnut Ridge Rd.                         */
  6. /*                        Grantsville, MD 21536-9801                      */
  7. /*                        --------------------------                      */
  8. /*                        Email:  thegrendel@aol.com                      */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package              */
  11. /*                                                                        */
  12. /**************************************************************************/
  13.  
  14.  
  15. #include <conio.h>
  16. #include "srch.h"
  17.  
  18.  
  19. #define FILE_OPENING_ERROR 3
  20. #define FILENAME_MAXLEN 8
  21. #define CR "\n"
  22. #define FILE_SUFFIX ".set"
  23. #define MAXLEN 30
  24. #define LINE_LEN 80
  25. #define NOARGS 1
  26. #define INCREMENT 1
  27. #define SPACE ' '
  28. #define WD 8
  29. #define SINGLE 1
  30.  
  31. #define BUFFERSIZE 8192
  32.  
  33. char ad[] =
  34. "INSET utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  35.  
  36.  
  37. void getword( char *lset, char *filename );
  38. void center( char *strng );
  39.  
  40. typedef enum { FALSE, TRUE } Boolean;
  41.  
  42. void main( int argc, char **argv )
  43. {
  44.  
  45.    char letterset [ MAXLEN ],
  46.         filename [ MAXLEN ];
  47.  
  48.      if( argc == NOARGS )
  49.         {
  50.         clrscr();
  51.         puts("Enter a LETTERSET to test ... ");
  52.         gets( letterset );
  53.      strcpy( filename, "word.lst" );
  54.         }
  55.      else
  56.      if( argc == NOARGS + 1 )
  57.            {
  58.         strcpy( letterset, *( argv + 1 ) );
  59.         strcpy( filename, "word.lst" );
  60.         }
  61.    else
  62.      {
  63.      strcpy( letterset, *( argv + 1 ) );
  64.      strcpy( filename,  *( argv + 2 ) );
  65.      }
  66.  
  67.      getword( letterset, filename );
  68. }
  69.  
  70.  
  71.  
  72. /**********************************WORDTEST********************************/
  73. /*       Function tests if word is constructible from Letterset           */
  74. /*                 Args in: char *letterset, char *word                   */
  75. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  76. /**************************************************************************/
  77.  
  78. Boolean wordtest( char *letterset, char *word )
  79. {
  80.     Boolean error_flag;
  81.  
  82.       if( strstr( word, letterset ) )
  83.          error_flag = TRUE;
  84.       else
  85.          error_flag = FALSE;
  86.         return( error_flag );
  87. }
  88.  
  89. /*************************************************************/
  90. void getword( char *letter_set, char *filename )
  91. {
  92.  
  93.     char    l_set [ MAXLEN ],
  94.         word [ MAXLEN ],
  95.         tempstr [ MAXLEN + 1 ],
  96.         targetfile [ MAXLEN ],
  97.         bar [ LINE_LEN + 1 ],
  98.    wd [ WD ],
  99.         double_bar [ LINE_LEN + 1 ];
  100.  
  101.     FILE *fptr,
  102.         *tfile;
  103.     int fnamelen;
  104.     long wcount = 0L;
  105.  
  106.        memset( bar, '-', LINE_LEN );
  107.        *( bar + LINE_LEN ) = NULL;
  108.        memset( double_bar, '=', LINE_LEN );
  109.        *( double_bar + LINE_LEN ) = NULL;
  110.  
  111.        /*************opening credits*************/
  112.        clrscr();
  113.        printf( double_bar );
  114.        strcpy( tempstr, ad );
  115.        center ( tempstr );
  116.        printf( tempstr );
  117.        printf( CR );
  118.        printf( double_bar );
  119.        printf( CR );
  120.        /****************************************/
  121.  
  122.  
  123.        strcpy ( l_set, letter_set );
  124. //       strcat ( letter_set, CR );
  125.  
  126.        /*   Create name of file to store derived words in   */
  127.        /*********************************************************/
  128.        fnamelen = strlen( l_set );
  129.        if( fnamelen  > FILENAME_MAXLEN )
  130.           fnamelen = FILENAME_MAXLEN;
  131.        strncpy( targetfile, l_set, fnamelen );
  132.        *( targetfile + fnamelen ) = NULL;
  133.        //NULL-terminate string, so strcat works, ha, ha.
  134.        strcat( targetfile, FILE_SUFFIX );
  135.        /*********************************************************/
  136.  
  137.        if( !( fptr = fopen( filename, "rt" ) ) )
  138.          {
  139.          printf( "\7\7\7Cannot open Wordfile!" );
  140.          exit( FILE_OPENING_ERROR );
  141.          }
  142.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  143.          exit( FILE_OPENING_ERROR + 1 );
  144.  
  145.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  146.          {
  147.          printf( "\7\7\7Cannot open file to save words in!" );
  148.          exit ( FILE_OPENING_ERROR + 2 );
  149.          }
  150.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  151.          exit( FILE_OPENING_ERROR + 3 );
  152.  
  153.        /**************'Wait' Message************/
  154.        printf( CR CR );
  155.        printf( "WORKING...\n\n" );
  156.        printf( "This will take a few seconds...\n" );
  157.        printf( "Please be patient.\n\n" );
  158.        printf( "Now searching 100,000+ word file \nand writing file of valid words containing -%s-.\n\n", 
  159.             letter_set );
  160.        /*****************************************/
  161.  
  162.  
  163.  
  164.  
  165.  
  166.        sprintf( tempstr, "Word(s) created from: %s\n", strupr( l_set ) );
  167.        center( tempstr );
  168.        fprintf( tfile, double_bar );
  169.       fprintf( tfile, CR );
  170.        fprintf( tfile, tempstr );
  171.        fprintf( tfile, double_bar );
  172.        fprintf( tfile, CR );
  173.  
  174.  
  175.          /*********************Main Loop*************/     
  176.           while( fgets( word, MAXLEN, fptr ) != NULL )
  177.  
  178.             if( wordtest( letter_set, word ) )
  179.                {
  180.                fprintf( tfile, "%s", word );
  181.                wcount++;
  182.                }
  183.           /*******************************************/
  184.  
  185.       if( wcount == SINGLE )
  186.           strcpy( wd, "word" );
  187.       else 
  188.           strcpy( wd, "words" );
  189.  
  190.           fprintf( tfile, bar );
  191.       fprintf( tfile, CR );
  192.           sprintf( tempstr, "%ld %s can be constructed from %s.",
  193.                  wcount, wd, l_set );
  194.           center( tempstr );              
  195.           fprintf( tfile, tempstr );
  196.           fprintf( tfile, "\n\n" );
  197.  
  198.           center( ad );
  199.           fprintf( tfile, ad );
  200.  
  201.           fcloseall();
  202.  
  203.           sprintf( tempstr,
  204.                  "The file %s has %ld %s derived from %s\7.",
  205.                  targetfile, wcount, wd, l_set );
  206.           center( tempstr );
  207.           printf( CR CR );
  208.           printf( tempstr );
  209.  
  210. }
  211.  
  212.  
  213.  
  214. void center( char *str )
  215. {
  216.    int padding;
  217.    char st [ LINE_LEN + INCREMENT ];
  218.  
  219.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  220.      memset( st, SPACE, padding );
  221.      *( st + padding ) = NULL;  //Terminate string
  222.      strcat( st, str );
  223.      strcpy( str, st );
  224.  
  225.      return;
  226. }
  227.